package com.ghn.cocknovel.ui.activity.map

import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.basemodel.base.BaseFragment
import com.ghn.cocknovel.BR
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.FragmentMappassengerBinding
import com.ghn.cocknovel.ext.MappageLoadBase
import com.ghn.cocknovel.ui.activity.map.vm.MapViewModel
import com.ghn.cocknovel.utils.OnTimerChangeListener
import com.ghn.cocknovel.utils.TimeTaskUtils
import com.ghn.cocknovel.view.DialogClickListener
import com.ghn.cocknovel.view.DialogMessage
import com.kt.NetworkModel.bean.BQueryDriverTransitOrderListBean


class MapPassengerFragment(val siteLatitude: Double, val siteLongitude: Double, val mTaskid: String) : BaseFragment<FragmentMappassengerBinding, MapViewModel>() {
    val mWorkAdapter by lazy { MapPassengerAdapter() }
    private val mOverTimeTask: HashMap<Int, TimeTaskUtils> by lazy { HashMap() }
    override fun initVariableId(): Int {
        return BR.mode
    }

    override fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): Int {
        return R.layout.fragment_mappassenger
    }

    override fun initParam() {
    }

    override fun initViewObservable() {

        viewModel?.queryTransitOrder()
        viewModel?.queryPassengerBySite?.observe(this) {
            viewModel?.squeryDriverTransitOrderList(it?.siteId.toString(), 1, mTaskid)
        }
        viewModel?.queryDriverTransitOrderList?.observe(this) {
            binding?.mRefresh?.finishRefresh()
            MappageLoadBase(it, mWorkAdapter)
        }
        mWorkAdapter.bindToRecyclerView(binding?.RecommRecyclerview)
        binding?.mRefresh?.setOnRefreshListener {
            viewModel?.queryTransitOrder()
        }

        viewModel!!.cancelTimeoutOrder.observe(this@MapPassengerFragment) { it1 ->
            if (it1?.equals("订单取消成功")!!) {
                showMsg(it1)
            }
            val activity = activity
            if (activity is MapCentenActivity) {
                activity.queryTransitOrder()
            }
            viewModel?.squeryDriverTransitOrderList(viewModel?.queryPassengerBySite?.value?.siteId!!, 1, mTaskid)
        }
        viewModel!!.onTransit.observe(this@MapPassengerFragment) { it1 ->
            if (it1?.equals("乘客已上车")!!) {
                showMsg(it1)
            }
            viewModel?.squeryDriverTransitOrderList(viewModel?.queryPassengerBySite?.value?.siteId!!, 1, mTaskid)
        }
    }

    inner class MapPassengerAdapter : BaseQuickAdapter<BQueryDriverTransitOrderListBean, BaseViewHolder>, BaseQuickAdapter.OnItemChildClickListener {
        constructor() : super(R.layout.item_home, null) {
            onItemChildClickListener = this
        }

        @SuppressLint("ResourceType")
        override fun convert(helper: BaseViewHolder, item: BQueryDriverTransitOrderListBean) {
            helper?.setText(R.id.item_home_author, "尾号" + item?.mobileLastFour)
            helper?.setText(R.id.item_home_originAddress, item?.originAddress)
            helper?.setText(R.id.item_home_destinationAddress, item?.destinationAddress)
            helper?.setText(R.id.item_home_PassengerNumber, "成人" + item?.adultNum + "人," + "儿童" + item?.childrenNum + "人")
            helper.setVisible(R.id.item_home_scar, false)
            helper.setVisible(R.id.item_home_cancel, false)
            val cancelTextView = helper.getView<TextView>(R.id.item_home_cancel)

            cancelTextView.setTextColor(resources.getColor(R.color.colorRed))
            cancelTextView.background.setTint(resources.getColor(R.color.color_bbbbbb))
            when (item?.orderStatus) {
                1 -> {
                    helper?.setText(R.id.item_home_status, "待支付")
                    helper.setVisible(R.id.item_home_cancel, true)
                }

                2 -> {
                    helper?.setText(R.id.item_home_status, "待呼叫")
                    helper.setVisible(R.id.item_home_cancel, true)
                }

                3 -> {
                    helper?.setText(R.id.item_home_status, "呼叫中")
                    helper.setVisible(R.id.item_home_cancel, true)
                }

                4 -> {
                    helper?.setText(R.id.item_home_status, "司机已接单")
                    helper.setVisible(R.id.item_home_scar, true)
                    helper.setVisible(R.id.item_home_cancel, true)
                }

                5 -> {
                    helper?.setText(R.id.item_home_status, "司机到达")
                    helper.setVisible(R.id.item_home_scar, true)
                    helper.setVisible(R.id.item_home_cancel, true)
                }

                6 -> {
                    helper?.setText(R.id.item_home_status, "已上车")
                }

                7 -> {
                    helper?.setText(R.id.item_home_status, "行程结束")
                }

                8 -> {
                    helper?.setText(R.id.item_home_status, "已取消")
                }

                9 -> {
                    helper?.setText(R.id.item_home_status, "已退款")
                }
            }
            // 倒计时 执行定时任务
            if (mOverTimeTask.get(helper.getBindingAdapterPosition()) == null && item.overplusTime > 0) {
                cancelTextView.isClickable = false
                cancelTextView.isEnabled = false
                val taskUtils = TimeTaskUtils().setChangeListener(object : OnTimerChangeListener() {

                    @SuppressLint("SetTextI18n")
                    override fun onTimerChange(millis: Long) {
                        super.onTimerChange(millis)
                        try {
                            if (millis != 0L) {
                                cancelTextView.text = "取消订单(${millis / 1000}秒)"
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                    }

                    override fun onTimerOver() {
                        super.onTimerOver()
                        cancelTextView.text = "取消订单"
                        cancelTextView.isClickable = true
                        cancelTextView.isEnabled = true
                        cancelTextView.setTextColor(resources.getColor(R.color.white))
                        cancelTextView.background.setTint(resources.getColor(R.color.colorRed))
                    }
                }).runTime(item.overplusTime)
                mOverTimeTask.put(helper.getBindingAdapterPosition(), taskUtils)
            }

            helper.addOnClickListener(R.id.item_home_scar)
            helper.addOnClickListener(R.id.item_home_cancel)
        }

        override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
            val item = adapter.getItem(position) as BQueryDriverTransitOrderListBean
            when (view.id) {
                R.id.item_home_scar -> {
                    viewModel?.getOnTransit(item.orderId, viewModel?.queryPassengerBySite?.value?.siteId?.toInt()!!, siteLatitude.toString(), siteLongitude.toString())
                }

                R.id.item_home_cancel -> {
                    DialogMessage(activity!!).setContext("取消订单？").setClickListener(object : DialogClickListener() {
                        override fun yesClick(dailog: Dialog) {
                            viewModel?.cancelTimeoutOrder(item.orderId)
                        }
                    }).show()
                }
            }

        }


    }

    override fun onDestroy() {
        super.onDestroy()
        mOverTimeTask.forEach {
            it.value.cancel()
        }
    }

}